//
//  FavouriteViewModel.swift
//  FeastBooking
//
//  Created by Imon on 2017/11/14.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import RxSwift
import Moya

class FavouriteViewModel {
    
    private var provider: RxMoyaProvider<FavouriteApi>
    
    init(token: String) {
        let endpointClosure = { (target: FavouriteApi) -> Endpoint<FavouriteApi> in
            let defaultEndpoint = RxMoyaProvider.defaultEndpointMapping(for: target)
            return defaultEndpoint.adding(newHTTPHeaderFields: ["content-type": "application/json", "Authorization": "Bearer \(token)"])
        }
        provider = RxMoyaProvider<FavouriteApi>(endpointClosure: endpointClosure)
    }
    
    func getFavouritesRestaurants(page: Int) -> Observable<[RestaurantSummaryResponse]> {
        return provider.request(.getFavouriteRestaurants(page: page))
               .mapJSON()
               .mapArray(type: RestaurantSummaryResponse.self)
    }
    
    func checkFavourite(id: Int) -> Observable<RestaurantSummaryResponse> {
        return provider.request(.checkFavourite(id: id))
               .mapJSON()
               .mapObject(type: RestaurantSummaryResponse.self)
    }
    
    func setFavourite(id: Int) -> Observable<String> {
        return provider.request(.setFavourite(id: id))
               .mapString()
    }
    
    func deleteFavourite(id: Int) -> Observable<String> {
        return provider.request(.deleteFavourite(id: id))
               .mapString()
    }
}
